package com.radar.RadarOnline;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import java.io.File;
import java.net.URL;

public class ROFeedParser {

    public static void main(String args[]) {

        try {

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
//            Document doc = dBuilder.parse(new InputSource(new URL("http://www.radaronline.com/feed").openStream()));
            Document doc = dBuilder.parse(new File("feed"));
            doc.getDocumentElement().normalize();

            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

            NodeList itemList = doc.getElementsByTagName("item");
            System.out.println("-----------------------");
 
            for (int items = 0; items < itemList.getLength(); items++) {
 
                Node itemNode = itemList.item(items);        
                if (itemNode.getNodeType() == Node.ELEMENT_NODE) {
 
                    System.out.println("Title: " + getTagValue("title", (Element)itemNode));
                    System.out.println("Link: " + getTagValue("link", (Element)itemNode));
                    System.out.println("Description: " + getTagValue("description", (Element)itemNode));
                    System.out.println("Comments: " + getTagValue("comments", (Element)itemNode));

                } 
            }


        } catch ( Exception e ) {
            e.printStackTrace();
        }
    }

    private static String getTagValue(String sTag, Element eElement){

        NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
        Node nValue = (Node) nlList.item(0); 
 
        return nValue.getNodeValue();    
    }

}